import {createRouter,createWebHistory,createWebHashHistory} from 'vue-router'
import {initHooks} from "./hooks"

///************************************************/

const constantRoutes = [{
    path: '/',
    name: 'root',
    component: () => import('@/views/layout/index.vue'),
    redirect: {
      name: "air"
    },
    meta: {
      id: "264009eadb1f2881dc44bc1a66d9d537",
    },
    children: [{
      path: 'air',
      name: 'air',
      component: () => import('@/views/pages/air/index.vue'),
      meta: {
        id: "f7ea6d2ec97d1fa71e5abd316dd85b53",
        activeMenu: 'air',
        title: '大气',
        icon: 'dashboard',
        affix: true
      }
    }]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/About.vue'),
    meta: {
      id: "c33b1fe779d86d9bd8b3c57ad4f80662",
      activeMenu: 'About',
      title: 'About',
      icon: 'dashboard',
      affix: true
    }
  }
]

///************************************************/

let name2path = {};
function updatePath(route, base) {
  let path = route.path;
  let item = name2path[route.name]
  if (item) {
    console.warn(`exists`, item)
  }
  if (!path || path === '*') {
    name2path[route.name] = path
    return;
  }
  base = base || '/'
  if (!base.endsWith('/')) {
    base = base + "/"
  }
  if (!path.startsWith("/")) {
    path = base + path
  }
  route.path = path;

  name2path[route.name] = route.path

  let children = route.children;
  if (children) {
    for (let i = 0; i < children.length; ++i) {
      updatePath(children[i], path)
    }
  }
}

function updateRoutesPath(routes, base) {
  if (routes) {
    for (let i = 0; i < routes.length; ++i) {
      updatePath(routes[i], base)
    }
  }
}

/**/
///************************************************/

updateRoutesPath(constantRoutes, '/')

const routes = constantRoutes;

const router = createRouter({
  history: createWebHashHistory(),
  // history: createWebHistory(),
  routes
})

initHooks(router)

///************************************************/

export default router
